package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.mongodb.BeanUtil;
import com.hanhai.zrb.api.mongodb.MongoUtil;
import com.hanhai.zrb.model.project.ProjectDetail;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;

@Controller
@RequestMapping("mongodb")
public class MongodbTestController extends BaseController {

	@RequestMapping("test")
	public void test(HttpServletResponse response) throws IOException {
		DB db = MongoUtil.db();

		ProjectDetail projectDetail = buildProjectDetail();
		DBCollection projectDetailCollection = db
				.getCollection("projectDetail");

		add(projectDetailCollection, projectDetail);

		Long id = 1L;
		ProjectDetail projectDetailBefore = findById(projectDetailCollection,
				id);
		if (projectDetailBefore != null) {
			System.out.println("刚刚新增的projectDetail=" + projectDetailBefore);
		}

		projectDetailBefore.setAddress("new-address-didu");
		projectDetailBefore.setProjectName("new projectName");
		update(projectDetailCollection, projectDetailBefore);
		ProjectDetail projectDetailAfter = findById(projectDetailCollection, id);
		if (projectDetailAfter != null) {
			System.out.println("修改后的projectDetail=" + projectDetailAfter);
		}

		projectDetail.setId(2L);
		add(projectDetailCollection, projectDetail);
		List idList = Arrays.asList(1L, 2L);
		List<ProjectDetail> projectDetailList = findByIdList(
				projectDetailCollection, idList);
		if (projectDetailList != null) {
			for (ProjectDetail a : projectDetailList) {
				System.out.println(a);
			}
		}

		BasicDBObject removeAll = new BasicDBObject();
		projectDetailCollection.remove(removeAll);
	}

	// 增加
	public static DBCollection add(DBCollection projectDetailCollection,
			ProjectDetail projectDetail) {
		DBObject object = BeanUtil.bean2DBObject(projectDetail);
		WriteResult wr = projectDetailCollection.insert(object);
		CommandResult cr = wr.getLastError();
		System.out.println(cr);
		return projectDetailCollection;
	}

	// 构造测试数据
	private static ProjectDetail buildProjectDetail() {
		ProjectDetail projectDetail = new ProjectDetail();
		projectDetail.setId(1L);
		projectDetail.setAddress("wuhan");
		projectDetail.setProjectName("projectName");
		return projectDetail;
	}

	// 从集合中，根据ID查找
	public static ProjectDetail findById(DBCollection collection, Long id) {
		BasicDBObject searchProjectDetailById = new BasicDBObject();
		searchProjectDetailById.append("id", id);
		ProjectDetail projectDetailBefore = null;
		DBCursor cursor = collection.find(searchProjectDetailById);
		while (cursor.hasNext()) {
			DBObject articleObject = cursor.next();
			projectDetailBefore = objectToArticle(articleObject);

			String internalId = articleObject.get("_id").toString();
			projectDetailBefore.setMongoId(internalId);
		}
		cursor.close();
		return projectDetailBefore;
	}

	// 修改
	public static void update(DBCollection collection,
			ProjectDetail projectDetail) {
		if (projectDetail.getId() == null) {
			return;
		}

		BasicDBObject updateCondition = new BasicDBObject();
		updateCondition.append("id", projectDetail.getId());

		DBObject newObject = BeanUtil.bean2DBObject(projectDetail);

		DBObject updateSetValue = new BasicDBObject("$set", newObject);
		collection.update(updateCondition, updateSetValue);
	}

	// 根据ID集合查找
	public static List<ProjectDetail> findByIdList(DBCollection collection,
			List<Long> idList) {
		BasicDBList values = new BasicDBList();
		values.addAll(idList);

		DBObject inQuery = new BasicDBObject("$in", values);

		DBObject con = new BasicDBObject();
		con.put("id", inQuery);
		DBCursor cursorIdArray = collection.find(con);

		List<ProjectDetail> projectDetailList = new ArrayList<ProjectDetail>();
		while (cursorIdArray.hasNext()) {
			DBObject articleObject = cursorIdArray.next();
			ProjectDetail projectDetail = new ProjectDetail();
			BeanUtil.dbObject2Bean(articleObject, projectDetail);
			String mongoId = articleObject.get("_id").toString();
			projectDetail.setMongoId(mongoId);

			projectDetailList.add(projectDetail);
		}
		return projectDetailList;
	}

	// 对象转换
	private static ProjectDetail objectToArticle(DBObject object) {
		if (object == null) {
			return null;
		}
		ProjectDetail projectDetail = new ProjectDetail();
		// 用工具方法转换，手动转换，需要判断类型，比较麻烦
		projectDetail = BeanUtil.dbObject2Bean(object, projectDetail);
		return projectDetail;
	}

}
